//
//  PayFaceToFaceTableViewController.m
//  消费100
//
//  Created by eliot on 16/4/1.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "PayFaceToFaceTableViewController.h"
#import "ImageTableViewCell.h"
#import "SpendAtStoreTableViewController.h"
@interface PayFaceToFaceTableViewController ()
/** 背景视图*/
@property (nonatomic, strong) UIView *blackView;
@property (nonatomic) BOOL isSelectedRightBarBtn;
@property (nonatomic, strong) UIView *swapView;



@property (nonatomic, strong) UIWebView *webView;


@end

@implementation PayFaceToFaceTableViewController

-(UIView *)blackView {
    if (!_blackView) {
        //灰色背景
        self.blackView = [[UIView alloc] init];
        [self.view addSubview:_blackView];
        
        self.blackView.frame = CGRectMake(0, 0, Width, Height);
        self.blackView .backgroundColor = [UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:0.4];
    }
    return _blackView;
}

+(PayFaceToFaceTableViewController*)CreatPayFaceToFaceTableViewController{
    
    UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    PayFaceToFaceTableViewController *PayFaceToFaceTableViewController=[story instantiateViewControllerWithIdentifier:@"PayFaceToFaceTableViewController"];
    
    return PayFaceToFaceTableViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"支付";
    self.tableView.tableFooterView = [[UIView alloc] init];
    /** 设置导航啦样式*/
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:19],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.navigationController.navigationBar.translucent = NO;
    self.tableView.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    [self.tableView registerNib:[UINib nibWithNibName:@"ImageTableViewCell" bundle:nil] forCellReuseIdentifier:@"imageCell"];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_scan"] style:UIBarButtonItemStylePlain target:self action:@selector(clickRightBtn)];
    self.navigationItem.rightBarButtonItem = rightBtn;
    
}

/**  点击扫一扫*/
- (void)clickRightBtn {
    self.isSelectedRightBarBtn = !self.isSelectedRightBarBtn;
    if (self.isSelectedRightBarBtn) {
        NSLog(@"1");
        [self addRightAddView];
        self.blackView.hidden = NO;
        NSLog(@"%f, %f", self.blackView.frame.size.height, self.blackView.frame.size.width);
        
    }else {
        NSLog(@"2");
        [self.swapView removeFromSuperview];
        self.blackView.hidden = YES;
    }
}

- (void)addRightAddView {
     NSLog(@"3");
    self.swapView = [[UIView alloc] init];
    self.swapView.layer.cornerRadius = 10;
    self.swapView.layer.masksToBounds = YES;
    self.swapView.backgroundColor = [UIColor whiteColor];
    [self.blackView addSubview:self.swapView];
    [self.swapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(100, 96));
    }];
//    self.swapView.frame = CGRectMake(Width - 15, 0, 100, 96);
    
    
    
    
    
    UIImageView *shareIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"iocn_alipay"]];
    [self.swapView addSubview:shareIV];
    shareIV.contentMode =  UIViewContentModeScaleAspectFit;
    [shareIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(23.0/2);
        make.left.mas_equalTo(23.0/2 - 2);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(25);
    }];
    UILabel *lb = [[UILabel alloc] init];
    [self.swapView addSubview:lb];
    lb.backgroundColor = [UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1];
    [lb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(47.5);
        make.height.mas_equalTo(1);
    }];
    
    UILabel *lb1 = [[UILabel alloc] init];
    lb1.text = @"扫一扫";
    [self.swapView addSubview:lb1];
    lb1.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
    [lb1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(shareIV.mas_right).mas_equalTo(4);
        make.top.right.mas_equalTo(0);
        make.height.mas_equalTo(47);
    }];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.swapView addSubview:btn1];
    btn1.backgroundColor = [UIColor clearColor];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(47);
    }];
    
    
    UIImageView *scanIV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_wechat"]];
    [self.swapView addSubview:scanIV];
    [scanIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-23.0/2);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(25);
        make.left.mas_equalTo(23.0/2 - 2);
    }];
    scanIV.contentMode =  UIViewContentModeScaleAspectFit;
    
    
    UILabel *lb2 = [[UILabel alloc] init];
    lb2.text = @"扫一扫";
    [self.swapView addSubview:lb2];
    lb2.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
    [lb2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(scanIV.mas_right).mas_equalTo(4);
        make.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(47.5);
    }];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.swapView addSubview:btn2];
    btn2.backgroundColor = [UIColor clearColor];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(47.5);
    }];
    [btn1 addTarget:self action:@selector(shareBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [btn2 addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

/** 支付宝扫一扫*/
- (void)shareBtnClick {
    
    self.blackView.hidden = YES;
    self.swapView.hidden = YES;
    
}
/** 微信扫一扫*/
- (void)scanBtnClick {
    
    self.blackView.hidden = YES;
    self.swapView.hidden = YES;
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }else if (section == 1) {
        return 2;
    }else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
            {
                ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell" forIndexPath:indexPath];
                cell.ivW.constant = Width;
                cell.ivH.constant = 196/400.0*Width;
                cell.iconIV.image = [UIImage imageNamed:@"wem"];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }
                break;
            case 1:
            {
                UITableViewCell *phoneCell = [tableView dequeueReusableCellWithIdentifier:@"phoneCell"];
                if (!phoneCell) {
                    phoneCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"phoneCell"];
                }
                phoneCell.selectionStyle = UITableViewCellSelectionStyleNone;
                phoneCell.textLabel.text = @"探鱼(天娱广场)";
                phoneCell.detailTextLabel.text = @"美食，烤鱼";
                phoneCell.textLabel.textColor = [UIColor colorWithRed:88/255.0 green:88/255.0 blue:88/255.0 alpha:1];
                phoneCell.detailTextLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
                UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_phone-1"]];
                iv.contentMode = UIViewContentModeScaleAspectFit;
                phoneCell.accessoryView = iv;
                UILabel *bgLb = [[UILabel alloc] init];
                bgLb.backgroundColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
                [phoneCell addSubview:bgLb];
                bgLb.frame = CGRectMake(Width - 60, 10, 1, 40);
                return phoneCell;
            }
                break;
            default:
            {
                
                UITableViewCell *weizhiCell = [tableView dequeueReusableCellWithIdentifier:@"weizhiCell"];
                if (!weizhiCell) {
                    weizhiCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"weizhiCell"];
                }
                weizhiCell.selectionStyle = UITableViewCellSelectionStyleNone;
                UILabel *weizhiLb = [[UILabel alloc] init];
                [weizhiCell addSubview:weizhiLb];
                weizhiLb.text = @"广州市天河区天河路623号天娱广场5楼B区";
                weizhiLb.frame = CGRectMake(15, 0, Width - 100, 44);
                UILabel *bgLb = [[UILabel alloc] init];
                bgLb.backgroundColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
                [weizhiCell addSubview:bgLb];
                bgLb.frame = CGRectMake(Width - 60, 5, 1, 30);
                weizhiLb.textColor = [UIColor colorWithRed:88/255.0 green:88/255.0 blue:88/255.0 alpha:1];
                UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_adder"]];
                iv.contentMode = UIViewContentModeScaleAspectFit;
                weizhiCell.accessoryView = iv;
                return weizhiCell;
            }
                break;
        }
    }else if (indexPath.section == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (indexPath.row == 1) {
            cell.textLabel.text = @"商家促销";
            UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_arrows"]];
            cell.textLabel.textColor = [UIColor colorWithRed:88/255.0 green:88/255.0 blue:88/255.0 alpha:1];
            cell.detailTextLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
            iv.frame = CGRectMake(0, 0, 12, 12);
            iv.contentMode = UIViewContentModeScaleAspectFit;
            cell.accessoryView = iv;
            
        }else {
            cell.textLabel.text = @"图文详情";
            UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_arrows"]];
            iv.frame = CGRectMake(0, 0, 12, 12);
            cell.textLabel.textColor =[UIColor colorWithRed:88/255.0 green:88/255.0 blue:88/255.0 alpha:1];
            cell.detailTextLabel.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
            iv.contentMode = UIViewContentModeScaleAspectFit;
            cell.accessoryView = iv;
        }
        return cell;
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lastCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"lastCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [cell addSubview:btn];
        btn.backgroundColor = [UIColor colorWithRed:255/255.0 green:88/255.0 blue:88/255.0 alpha:1];
        [btn setTitle:@"面对面支付" forState:UIControlStateNormal];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(25);
            make.right.mas_equalTo(-25);
            make.top.mas_equalTo(5);
            make.height.mas_equalTo(35);
        }];
        btn.layer.cornerRadius = 8;
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(faceToFacePay) forControlEvents:UIControlEventTouchUpInside];
        UILabel *centerLb = [[UILabel alloc] init];
        centerLb.text = @"欢迎到店消费，赚取金币增值，APP上买单尊享更多优惠";
        centerLb.textColor = [UIColor colorWithRed:136/255.0 green:136/255.0 blue:136/255.0 alpha:1];
        [cell addSubview:centerLb];
        [centerLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(btn.mas_bottom).mas_equalTo(5);
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(15);
        }];
        centerLb.font = [UIFont systemFontOfSize:12];
        centerLb.textAlignment = NSTextAlignmentCenter;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                return  196/400.0*Width;
            }else if (indexPath.row == 1) {
                return 60;
            }else {
                return 44;
            }
        }else if (indexPath.section == 1) {
            return 44;
        }else {
            return 70;
        }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://10086"]];
//            // 提示：不要将webView添加到self.view，如果添加会遮挡原有的视图
            // 懒加载
            if (_webView == nil) {
                _webView = [[UIWebView alloc] init];
            }
            NSURL *url = [NSURL URLWithString:@"tel://10010"];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            
            [_webView loadRequest:request];
        }
    }
}


/** 面对面支付*/
- (void)faceToFacePay {
    SpendAtStoreTableViewController *vc = [SpendAtStoreTableViewController CreatSpendAtStoreTableViewController];
    [self creatNav:vc];
}


//创建模态导航控制器
-(void)creatNav:(UIViewController *)view{
    
    UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:view];
    nav.navigationBar.barTintColor = [UIColor colorWithRed:254/255.0 green:154/255.0 blue:37/255.0 alpha:1];
    view.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_back"] style:UIBarButtonItemStylePlain target:self action:@selector(dismiss)];
    [self presentViewController:nav animated:YES completion:nil];
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}



/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
